
<template>
  <view class="page-container">
    <scroll-view scroll-y="true" class="page-body py-32">
       <view class="box-shadows py-20 px-32 flex items-center justify-between" v-if="loading&&collectList.length>0" @click.stop="submit">
          <view>全部收藏</view>
          <view class="flex items-center">{{collectList.length}}  <image class="w-20 h-28 ml-12" src="/static/images/right.png" /></view>
       </view>
      <view class="box-shadows py-20 px-32 flex items-center justify-between" v-if="loading&&errList.length>0" @click.stop="submit">
        <view>全部错题</view>
        <view class="flex items-center">{{errList.length}}  <image class="w-20 h-28 ml-12" src="/static/images/right.png" /></view>
      </view>
      <view class="box-shadows py-20 px-32 flex items-center justify-between mt-32" v-if="loading&&errList.length>0">
        <view>答对后自动移除错题记录</view>
        <switch class="ml-12" :checked="isShowError" @change="switchChange" />
      </view>
      <up-empty marginTop="100" :text="isotype===3?'你收藏的题目会自动记录到这里':'暂无错题记录'" v-if="(loading&&collectList.length==0) && (loading&&errList.length==0)"></up-empty>
    </scroll-view>
    <view   @click="submit" class="font-32 text-ff bg-primary flex items-center justify-center h-100">去练习</view>
  </view>
</template>
<script setup>
import { ref} from 'vue'
import {onLoad} from "@dcloudio/uni-app"
import utils from '@/utils/utils.js'
import  {subject} from '@/api'
import useUserStore from '@/store/user.js'
const useStore = useUserStore()
import useExam from '@/common/useExam.js'
const {
  isShowError,
  storageArr,
  titleType,
} = useExam();
const isotype = ref('')
const categoryId = ref('')
const collectList = ref([])
const errList = ref([])
const loading = ref(false)
onLoad((options)=>{
  console.log('options:', options)
  if (options.type) {
    isotype.value = Number(options.type)
    uni.setNavigationBarTitle({title: titleType[isotype.value]})
   }
  if (options.categoryId) {
    categoryId.value = Number(options.categoryId)
    getStorageApi()
  }
})
const switchChange = (e) => {
  isShowError.value = e.detail.value
  console.log('switchChange:', isShowError.value)
}
//获取接口缓存的记录
const getStorageApi = async () => {
  try{
    loading.value = false
    const res = await subject.getSubjectPracticeList({
      userId: Number(useStore.uid),
      categoryId: Number(categoryId.value),
    })
    if (res.code === 0) {
      storageArr.value = res.data || []
      console.log('接口缓存', storageArr.value)
      if(isotype.value === 3){
        //我的收藏
        const obj = storageArr.value.find(item => item.storageKey === "starids2")
        if(obj){
          collectList.value = JSON.parse(obj.storageValue)
        }
        console.log('我的收藏', collectList.value)
      } else if(isotype.value === 4){
        //我的错题
        const obj = storageArr.value.find(item => item.storageKey === "errorids2")
        if(obj){
          errList.value = JSON.parse(obj.storageValue)
        }
        console.log('我的错题', errList.value)
      }
    }
  }finally{
    loading.value = true
  }
}
const submit = () => {
  if(collectList.value.length===0 && errList.value.length===0){
    uni.redirectTo({
      url: '/pages/index/practice/index?type=' + 1
    })
  }else {
    uni.redirectTo({
      url: `/pages/template/index?categoryId=${categoryId.value}&type=1&isotype=${isotype.value}`
    })
  }

}
</script>
<style scoped lang="scss">
</style>